package com.qianxx.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 */
public final class DateUtil {

    /**
     * 将Date类型转换为日期字符串
     *
     * @param date Date对象
     * @param type 需要的日期格式
     * @return 按照需求格式的日期字符串
     */
    public static String formatDate(Date date, String type) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(type);
            return df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getTodayOrTomorrow(long time) {
        Date date = new Date(time);
        //date 是存储的时间戳
        //所在时区时8，系统初始时间是1970-01-01 80:00:00，注意是从八点开始，计算的时候要加回去
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (time + offSet) / 86400000;
        long intervalTime = start - today;
        //0：今天,1：明天,2：后天
        String strDes = "";
        if (intervalTime == 0) {
            strDes = "今天";//今天
            strDes += formatDate(date, "HH:mm");
        } else if (intervalTime == 1) {
            strDes = "明天";//明天
            strDes += formatDate(date, "HH:mm");
        } else if (intervalTime == 2) {
            strDes = "后天";//后天
            strDes += formatDate(date, "HH:mm");
        } else {
            strDes = formatDate(date, "MM月dd日HH:mm");
        }
        return strDes;
    }

    /**
     * 把毫秒转化成日期
     *
     * @param dateFormat (日期格式，例如：MM/dd/yyyy HH:mm:ss)
     * @param millSec    (毫秒数)
     * @return
     */
    public static String transferLongToDate(String dateFormat, long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * 星期几
     *
     * @param date Date 日期
     * @return 星期一到星期日
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    public static String getAppointmentTime(long time) {
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (time + offSet) / 86400000;
        long intervalTime = start - today;
        if (intervalTime == 0) {
            if (time - System.currentTimeMillis() > 1800000) {
                return "今天 " + formatDate(new Date(time), "HH:mm");
            } else {
                return "即将开始 " + formatDate(new Date(time), "HH:mm");
            }
        } else if (intervalTime == 1) {
            return "明天 " + formatDate(new Date(time), "HH:mm");
        } else if (intervalTime == 2) {
            return "后天 " + formatDate(new Date(time), "HH:mm");
        } else {
            return formatDate(new Date(time), "yyyy-MM-dd HH:mm");
        }
    }

    public static String getPoolTodayOrTomorrow(long time) {
        Date date = new Date(time);
        //date 是存储的时间戳
        //所在时区时8，系统初始时间是1970-01-01 80:00:00，注意是从八点开始，计算的时候要加回去
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (time + offSet) / 86400000;
        long intervalTime = start - today;
        //0：今天,1：明天,2：后天
        String strDes = "";
        if (intervalTime == 0) {
            strDes = "今天";//今天
        } else if (intervalTime == 1) {
            strDes = "明天";//明天
        } else if (intervalTime == 2) {
            strDes = "后天";//后天
        } else {
            strDes = formatDate(date, "MM月dd日HH:mm");
        }
        return strDes;
    }
}
